package ${conf.basePackage}.${table.camelName}.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.king.base.controller.BaseController;
import com.king.base.controller.ModelResult;
import ${conf.basePackage}.${table.camelName}.model.${table.className};
import ${conf.basePackage}.${table.camelName}.service.${table.className}Service;

/**
 * ${table.comment}
 * @author ${conf.author}
 * @version ${conf.version}
 * @since ${conf.createDate}
 */
@Controller
@RequestMapping("/${table.camelName}")
public class ${table.className}Controller extends BaseController {

	@Autowired
	private ${table.className}Service ${table.camelName}Service;

	@RequestMapping(value = "list", method = RequestMethod.GET)
	@ResponseBody
	public ModelResult list(Map<String, Object> params,
							@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
							@RequestParam(value = "pageSize", required = true, defaultValue = "10") Integer pageSize) {
		int totalCount = ${table.camelName}Service.count(params);
		List<${table.className}> ${table.camelName}List = ${table.camelName}Service.page(params, (pageNo - 1 < 0 ? -1 : pageNo - 1) * pageSize, pageSize);
		return responsePage(ModelResult.CODE_200, ModelResult.SUCCESS, totalCount, ${table.camelName}List);
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public ModelResult save(${table.className} ${table.camelName}) {
		int result = ${table.camelName}Service.save(${table.camelName});
		return responseMessage(result == 1 ? ModelResult.CODE_200 : ModelResult.CODE_500, result == 1 ? ModelResult.SUCCESS : ModelResult.FAIL);
	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	@ResponseBody
	public ModelResult edit(@RequestParam(value = "id", required = true) Long id) {
		${table.className} ${table.camelName} = ${table.camelName}Service.findById(id);
		return responseEntity(ModelResult.CODE_200, ModelResult.SUCCESS, ${table.camelName});
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public ModelResult update(${table.className} ${table.camelName}) {
		int result = ${table.camelName}Service.modify(${table.camelName});
		return responseMessage(result == 1 ? ModelResult.CODE_200 : ModelResult.CODE_500, result == 1 ? ModelResult.SUCCESS : ModelResult.FAIL);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public ModelResult delete(@RequestParam(value = "id", required = true) Long id) {
		int result = ${table.camelName}Service.remove(id);
		return responseMessage(result == 1 ? ModelResult.CODE_200 : ModelResult.CODE_500, result == 1 ? ModelResult.SUCCESS : ModelResult.FAIL);
	}
}